//
//  ZAssignNotifyVC.m
//  WeClubs
//
//  Created by Zed Link on 17/4/2017.
//  Copyright © 2017 Zed. All rights reserved.
//

#import "ZAssignNotifyVC.h"
#import "ZTodoNotifyCell.h"
#import "ZTodoFlowLayout.h"
#import "ZNewNotifyVC.h"
#import "ZNoticeModel.h"
#import "ZTodoNotifyDetailVC.h"
static NSString *AssignNotifyCellReuseID = @"AssignNotifyCellReuseID";

@interface ZAssignNotifyVC () <UICollectionViewDelegate, UICollectionViewDataSource>

/** 指派通知列表 */
@property (weak, nonatomic) UICollectionView *notifyView;

/** 指派通知数据源 */
@property (strong, nonatomic) NSArray <ZTodoNotifyModel *> *notifyModels;

//通知数据源
@property(nonatomic)NSMutableArray *noticeDataArr;

@end

@implementation ZAssignNotifyVC

#pragma mark - View Life Cycle
- (void)viewDidLoad{

    [super viewDidLoad];
    
    [self setupNav];
    
    [self setupCollectionInit];
    
    [self loadDataFormNetWork];

}
#pragma mark - setupUI
- (void)setupNav{
    
    [self zAddBackItemWithGesture:YES];
    self.navigationItem.title = @"通知管理";
    self.navigationItem.rightBarButtonItem = [self zNavItemWithTitle:@"发起通知" andSelector:@selector(createMissionAction)];
}
- (void)setupCollectionInit{

    self.automaticallyAdjustsScrollViewInsets = NO;
    // ========== 待办通知列表 ==========
    ZTodoFlowLayout *layout = [[ZTodoFlowLayout alloc] init];
    
    UICollectionView *notifyView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
    notifyView.delegate = self;
    notifyView.dataSource = self;
    
    [notifyView setBackgroundColor:GENERAL_VIEW_BG_COLOR];
    [notifyView registerNib:[UINib nibWithNibName:@"ZTodoNotifyCell" bundle:nil] forCellWithReuseIdentifier:AssignNotifyCellReuseID];
    notifyView.contentInset = UIEdgeInsetsMake(DYNAMIC_TOP_SPACE, 0, DYNAMIC_TOP_SPACE, 0);
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    self.notifyView = notifyView;
    [self.view addSubview:notifyView];
    [self.notifyView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(UIEdgeInsetsMake(64, 0, 0, 0));
    }];

}

#pragma mark - loadData
- (void)loadDataFormNetWork{

    [MBProgressHUD zAlertProgress:@"正在加载中"];
    [HTTPRequest POST:kGet_my_notifybUrl parameter:@{@"sponsor_id":@([ZUserInfo sharedInstance].userId)} success:^(id resposeObject) {
        
                [MBProgressHUD zAlertHideHUD];
        
        if ([resposeObject[@"result_code"] integerValue] == ZNetManagerStatusSuccess) {
            
            for (NSDictionary *dic in resposeObject[@"data"][@"notify"]) {
                
                ZNoticeModel * model = [[ZNoticeModel alloc]initWithDictionary:dic];
                
                [self.noticeDataArr addObject:model];
            }
            [self.notifyView reloadData];
            
        }else{
            [MBProgressHUD zAlertMessage:resposeObject[@"result_msg"] to:self.view afterDelay:1.0];
        
        }
    } failure:^(NSError *error) {
        
        [MBProgressHUD zAlertHideHUD];
    }];

}

#pragma mark - UICollectionViewDataSource & UICollectionViewDelegate

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{

    return self.noticeDataArr.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{

    ZTodoNotifyCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:AssignNotifyCellReuseID forIndexPath:indexPath];
    cell.type = ZNotifyTypeAssign;
    cell.noticeModel = self.noticeDataArr[indexPath.row];
    return cell;
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{

    [self.navigationController pushViewController:[[ZTodoNotifyDetailVC alloc]init] animated:YES];

}
#pragma mark - SEL Methods
- (void)createMissionAction{
    ZNewNotifyVC *newVC = [ZNewNotifyVC zNew];
    [self.navigationController pushViewController:newVC animated:YES];
}

#pragma mark - Setter && Getter Methods
- (NSMutableArray *)noticeDataArr{

    if (!_noticeDataArr) {
        _noticeDataArr = [NSMutableArray array];
    }
    
    return _noticeDataArr;

}
@end
